﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.Permission.Interface;
using Microhard.Permission;
using Microhard.Permission.Models;
using Microhard.Core.DBUtility;

namespace Microhard.Permission_MembershipImpl
{
    public class ResouceTypeImpl : IResourceType
    {

        #region IResourceType Members

        public bool IsInUsing(Guid resourceTypeID)
        {
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var rtn = db.MicroHard_Resources.Count(r => r.MicroHard_ResourceType.ResourceTypeID == resourceTypeID);
            if (rtn > 0)
                return true;
            return false;
        }

        #endregion

        #region IRepository<ResouceType> Members

        public ExecutedResult Add(ResouceType model)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var toAdd = model.ToEntity();
            toAdd.ResourceTypeID = Guid.NewGuid();

            db.MicroHard_ResourceTypes.InsertOnSubmit(toAdd);
            try
            {
                db.SubmitChanges();
                result.Scope.IsSysManage = true;
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add("ResouceTypeImpl.Add", ex.Message);
            }
            return result;
        }

        public ExecutedResult Update(ResouceType model)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var entity = db.MicroHard_ResourceTypes.SingleOrDefault(c => c.ResourceTypeID == model.ResourceTypeID);

            if (entity != null)
            {
                entity.Description = model.Description;
                entity.Name = model.Name;
                entity.KeyName = model.KeyName;
            }
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add("ResouceTypeImpl.Upate", ex.Message);
            }
            return result;
        }

        public ExecutedResult Delete(List<Guid> ids)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var toDeltes = from b in db.MicroHard_ResourceTypes
                           where ids.Contains(b.ResourceTypeID)
                           select b;
            foreach (var d in toDeltes)
            {
                //是否被使用
                if (d.MicroHard_Resources != null && d.MicroHard_Resources.Count > 0)
                {
                    result.ErrorMsg.Add(d.ResourceTypeID.ToString(), "资源类型:" + d.Name + "正在使用中，无法删除");
                    continue;
                }
                db.MicroHard_ResourceTypes.DeleteOnSubmit(d);
            }
            try
            {
                db.SubmitChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add("ResouceTypeImpl.Delete", ex.Message);
            }
            return result;
        }

        public ExecutedResult Delete(ResouceType model)
        {
            throw new NotImplementedException();
        }

        public ResouceType GetModelByID(Guid id)
        {
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            var entity = db.MicroHard_ResourceTypes.SingleOrDefault(b => b.ResourceTypeID == id);
            if (entity != null)
                return entity.ToModel();
            return null;
        }

        public ResouceType GetModelByModel(ResouceType model)
        {
            throw new NotImplementedException();
        }

        public List<ResouceType> Query(ResouceType Model, int pageIndex, int pageSize, out int totalCount)
        {
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            IEnumerable<MicroHard_ResourceType> entities = null;
            entities = from b in db.MicroHard_ResourceTypes
                       select b;
            totalCount = entities.Count();
            var pagedEntities = entities.OrderByDescending(p => p.Name).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
            return pagedEntities.Select(p => p.ToModel()).ToList();
        }

        public List<ResouceType> Query(ResouceType Model)
        {
            MicrohardOUGDBDataContext db = new MicrohardOUGDBDataContext();
            IEnumerable<MicroHard_ResourceType> entities = null;
            entities = from b in db.MicroHard_ResourceTypes
                       select b;
            return entities.Select(p => p.ToModel()).ToList();
        }

        #endregion

        #region IRepository<ResouceType> Members


        public ScopeResult scope
        {
            set;
            get;
        }

        #endregion
    }
}
